package com.tingyuge.base.thread.completion;

import java.util.concurrent.*;
import java.util.stream.IntStream;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/3/21
 */
public class Test1 {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 10, 10, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(20), new ThreadPoolExecutor.AbortPolicy()
        );
        ExecutorCompletionService completionService = new ExecutorCompletionService(executor);

        IntStream.range(0,10).forEach(i->{
            completionService.submit(()->{
                System.out.println(Thread.currentThread().getName());
                Thread.sleep((long) Math.random() *1000);
                return i*i;
            });
        });

        for (int i = 0; i < 10; i++) {
                System.out.println(completionService.take().get());


        }
        executor.shutdown();
    }
}
